<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 100px;
            height: 100px;
            background: green;
            position: fixed;
            left: 100px;
            top: 100px;
        }
    </style>
</head>

<body>
    <div class="box" id="box"></div>
    <script>
        const box = document.getElementById('box');
        window.onkeydown = function (e) {
            const pos = box.getBoundingClientRect();
            if (e.key === 'ArrowUp') {
                // 上
                box.style.top = pos.top - 10 + 'px';
            }
            if (e.key === 'ArrowDown') {
                // 下
                box.style.top = pos.top + 10 + 'px';
            }
            if (e.key === 'ArrowLeft') {
                // 左
                box.style.left = pos.left - 10 + 'px';
            }
            if (e.key === 'ArrowRight') {
                // 右
                box.style.left = pos.left + 10 + 'px';
            }

        }

    </script>
</body>

</html>